#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@Desc    :   提单号蜘蛛基类
"""

from typing import Union, Optional, Dict, List

from parsel import Selector
from src.enum.exception import ExceptionEnum
from src.enum.task import ExecResultEnum, CrawlerTypeEnum
from src.exception import CrawlerException, WaybillNumberInvalidException, WebpageLoadingErrorException, \
    CrawlerFailureException
from src.model.ocean_bill_of_lading import VoyageInfo,OblResult,ContainerInfo,ContainerEvent
from src.util import flogger

class OblSpiderBase:
    key = None  # 资源标识符

    log = flogger.Flogger().get_logger(__name__)

    def check_bill_of_lading(self, bill_of_lading: str) -> bool:
        return True

    def crawling_page(self,bill_of_lading: str, proxy=None, idx=0) -> Union[None, Dict, Selector]:
        return None

    def pick_base_info(self, data) -> Optional[OblResult]:
        return None

    def convert_result(self, data) -> OblResult:
        base_info: OblResult = self.pick_base_info(data)
        return base_info

    def run(self, bill_of_lading) -> OblResult:
        if not self.check_bill_of_lading(bill_of_lading):
            raise WaybillNumberInvalidException()
        self.log.info(f"{CrawlerTypeEnum.OCEAN_WAYBILL.name} crawler number: {bill_of_lading}({self.key}) run begin!")
        try:
            page_data = self.crawling_page(bill_of_lading)
        except CrawlerException as e:
            raise e
        except Exception as e:
            self.log.error(e)
            raise WebpageLoadingErrorException()
        return self.convert_result(page_data)


